package com.atguigu.CircleArrayQueue;


import java.util.Scanner;

/**
 * description:
 * Created by 才睡醒 on 2021-06-03
 */
public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        //创建一个队列
        CircleArray arrayQueue = new CircleArray(4);
        char key = ' ';
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出队列");
            System.out.println("a(add): 添加队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头部数据");
            key = sc.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("输入一个数");
                    int value = sc.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int result = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n", result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h'://查看队列头的数据
                    try {
                        int result = arrayQueue.headQueue();
                        System.out.printf("队列头的数据是%d\n", result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}


class CircleArray {
    private int maxSize;//表示数组的最大容量
    private int front; //队列头
    private int rear; //队列尾
    private int[] arr; //该数组用于存储数据，模拟队列

    //创建队列的构造器
    public CircleArray(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[arrMaxSize];
        front = 0;  //指向队列头部，指向队列第一个元素
        rear = 0;   //指向队列尾部，指向队列尾元素的后一个位置
    }

    //判断队列是否满
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    //判断队列是否空
    public boolean isEmpt() {
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int n) {
        //判断数据是否满
        if (isFull()) {
            System.out.println("队列满;不能添加数据");
            return;
        }
        //直接将数据存入
        arr[rear] = n;
        //将rear后移，这里考虑取模
        rear = (rear + 1) % maxSize;
    }

    //从队列获取数据
    public int getQueue() {
        if (isEmpt()) {
//            System.out.println("队列空，无法取出数据");
            //通过抛出异常
            throw new RuntimeException("队列空，不能取数据");
        }
        //取出数据
        int value = arr[front];
        //front后移取模
        front = (front + 1) % maxSize;
        return value;
    }

    //显示队列数据
    public void showQueue() {
        //判断队列是否为空
        if (isEmpt()) {
            System.out.println("队列空");
            return;
        }
        //遍历队列
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    //获取队列有效数据
    public int size() {
        if (isEmpt()) {
            return 0;
        }
        return (rear + maxSize - front) % maxSize;
    }

    //显示队列头数据，注意不是取出数据
    public int headQueue() {
        //判断
        if (isEmpt()) {
//            System.out.println("队列是空的");
            throw new RuntimeException("队列是空的，没有数据");
        }
        return arr[front];
    }
}
